package main

import (
	"fmt"
	"sync"
)

var down = sync.WaitGroup{}

type safeItem struct {
	content map[int]int
	lock    sync.RWMutex
}

func (safe *safeItem) addEntity(i int) {
	defer func() {
		if err := recover(); err != nil {
			fmt.Printf("出错了！i=%d,error=%v\n", i, err)
		} else {
			fmt.Println("已添加,i=", i)
		}
		down.Done()
	}()

	safe.lock.Lock()
	//放入延迟队列，及时后续代码出现异常后，也可以执行
	defer safe.lock.Unlock()
	safe.content[i] = i / (i - 10)
	//safe.lock.Unlock()
}

func main() {
	item := safeItem{}
	item.content = make(map[int]int, 10)
	down.Add(10)
	for i := 1; i <= 10; i++ {
		go func(k int) {
			item.addEntity(k)
		}(i)
	}
	down.Wait()
}
